<template>
    <div class="list-wrapper">
        <div class="list-top">
            <el-form :inline="true" :model="searchForm">
                <el-form-item label="容量">
                    <el-input v-model="searchForm.capacity" placeholder="请输入容量"></el-input>
                </el-form-item>
                <el-form-item label="面积">
                    <el-input v-model="searchForm.acreage" placeholder="请输入面积"></el-input>
                </el-form-item>
                <el-form-item label="是否有模拟器">
                    <el-checkbox-group v-model="searchForm.simulator">
                        <el-checkbox label="是"></el-checkbox>
                        <el-checkbox label="否"></el-checkbox>
                    </el-checkbox-group>
                </el-form-item>
                <el-form-item label="教室类型">
                    <el-select v-model="searchForm.type" placeholder="请选择" clearable>
                        <el-option v-for="item in type" :label="item" :value="item" :key="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search()">搜索</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="">
            <table class="list-table" border="0" cellspacing="0" cellpadding="0">
                <thead>
                <tr>
                    <th>
                        <span class="icon add" @click="OpenAddClassRoom()"></span>
                    </th>
                    <th>教室编号</th>
                    <th>教室名称</th>
                    <th>容量</th>
                    <th>面积</th>
                    <th>是否有模拟器</th>
                    <th>教室类型</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="item in ClassRoomData" :key="item.id">
                    <td>
                        <span class="icon del" @click="DelClassRoom(item.id)"></span>
                        <span class="icon det" @click="onDetail(item.id)"></span>
                    </td>
                    <td>{{item.number}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.capacity}}</td>
                    <td>{{item.acreage}}</td>
                    <td>{{item.simulator}}</td>
                    <td>{{item.type}}</td>
                </tr>
                </tbody>
            </table>
            <div class="page">
                <div class="block">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next, jumper"
                                   :total="pageTotal">
                    </el-pagination>
                </div>
            </div>
            <el-dialog title="新建培训教室信息" :visible.sync="pop" size="tiny">
                <el-form :model="addClassroom" :label-width="width" :rules="ClassRoomRulse" ref="ClassRoomRulesName">
                    <el-form-item label="教室编号" prop="number">
                        <el-col :span="span">
                            <el-input v-model="addClassroom.number" size="small" placeholder="请输入教室编号"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="教室名称" prop="name">
                        <el-col :span="span">
                            <el-input v-model="addClassroom.name" size="small" placeholder="请输入教室名称"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="容量">
                        <el-col :span="span">
                            <el-input v-model="addClassroom.capacity" size="small" placeholder="请输入容量"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="面积">
                        <el-col :span="span">
                            <el-input v-model="addClassroom.acreage" size="small" placeholder="请输入面积"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="是否有模拟器">
                        <el-col :span="span">
                            <el-radio-group v-model="addClassroom.simulator" size="small">
                                <el-radio label="是"></el-radio>
                                <el-radio label="否"></el-radio>
                            </el-radio-group>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="教室类型">
                        <el-col :span="span">
                            <el-select v-model="addClassroom.type" placeholder="请选择" size="small">
                                <el-option v-for="item in type" :label="item" :value="item" :key="item"></el-option>
                            </el-select>
                        </el-col>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="pop = false">取 消</el-button>
                    <el-button type="primary" @click="AddClassRoom()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    const addClassroomData = {
        number: '',
        name: '',
        capacity: '',
        acreage: '',
        simulator: '',
        type: '',
    };
    export default {
        data() {
            return {
                pageIndex: 1,
                pageTotal: 1,
                type: ['自用', '出租', '外借'],
                searchForm: {
                    capacity: '',
                    acreage: '',
                    simulator: [],
                    type: '',
                },
                width: '200px',
                span: 15,
                pop: false,
                addClassroom: _g.cloneJson(addClassroomData),
                ClassRoomData: '',
                ClassRoomRulse: {
                    name: [{required: true, message: '请填写名称', trigger: 'blur'}],
                    number: [{required: true, message: '请填写编号', trigger: 'blur'}]
                },
            }
        },

        created() {
            this.getClassRoomList();
        },

        methods: {
            search() {
                this.getClassRoomList()
            },
            onDetail(id) {
                router.push('/operate/assets/facilities/classroomDetail/' + id);
            },
            handleCurrentChange(val) {
                this.pageIndex = val;
                this.getClassRoomList();
            },
            getClassRoomList() {
                let data = this.searchForm;
                data.page = this.pageIndex;
                this.apiPost('Operation/classRoomList', data).then(res => {
                    this.ClassRoomData = res.list;
                    this.pageTotal = parseInt(res.total)
                })
            },
            AddClassRoom() {
                this.$refs.ClassRoomRulesName.validate((valid) => {
                    if (valid) {
                        this.pop = false;
                        let data = this.addClassroom;
                        this.apiPost('Operation/classRoomAdd', data).then(res => {
                            let remind = res.error ? 'error' : 'success';
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getClassRoomList();
                            } else {
                                this.pop = true
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },
            OpenAddClassRoom() {
                this.pop = true;
                this.addClassroom = _g.cloneJson(addClassroomData);
                this.$refs.ClassRoomRulesName.resetFields();
            },
            DelClassRoom(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let data = {id: id};
                    let cus = {id: this.id};
                    this.apiPost('Operation/classRoomDelete', data).then(res => {
                        if (!res.error) {
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg
                            });
                        }
                        this.getClassRoomList();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
        },
    }
</script>

<style scoped>
    .add {
        width: 25px;
        height: 25px;
        background: url(../../../../img/add.png) no-repeat;
        background-size: 100%;
        cursor: pointer;
    }

    .edit {
        background: url(../../../../img/edit.gif) no-repeat;
    }

    .del {
        background: url(../../../../img/del.png) no-repeat;
    }

    .det {
        background: url(../../../../img/detail.png) no-repeat;
        cursor: pointer;
    }
</style>
